*Replication Code for
*Treul, Sarah A. and Eric R. Hansen."Primary Barriers to Working Class Representation." Forthcoming at Political Research Quarterly.
*Compiled by Eric Hansen, 2/14/23 (ehansen4@luc.edu)
*Using Stata 16.1 IC

***********
*open data set
***********

clear

*remember to set your own working directory
*cd “C:\ ”

use "Treul_Hansen_Replication_Data.dta"

***********
*label variables
***********

label var worker_current_recode "Worker"
label var qualnumber "Quality Opponents"
label var openseat "Open Seat"
label var total_primary "Primary Funds (millions of \$)"
label var gop "Republican"
label var third "Third Party"
label var dist_income "District Median Income (in \$100,000)"
label var college "District College-Educated"
label var dem_vote "Democratic Vote Share"
label var ideo_extreme "Ideological Extremity"
label var candnumb "Number of Candidates"

***********
*Table 1
***********

ttest candpct, by(worker_current_recode)
ttest winwin, by(worker_current_recode)

***********
*Figure 1
***********

*panel A
quietly mixed candpct worker_current_recode || raceid:, vce(cluster raceid)
margins, at(worker_current_recode==(0 1))
marginsplot, recast(bar) title("") ytitle(Predicted Vote Share, margin(medium)) xtitle("") xlabel(0 "Nonworker" 1 "Worker") yscale(range(0(.2)1)) ylabel(0 0.2 0.4 0.6 0.8 1) xscale(range(-0.75(0.5)1.75)) graphregion(fcolor(white)) ciopts(col(gs4)) plot1opts(barw(0.3) color(gs8))

*panel B
quietly mixed candpct worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary gop third dist_income college dem_vote || raceid:, vce(cluster raceid)
margins, at(worker_current_recode==(0 1) qualnumber==1 openseat==1 quality_cand==0 gop==0 third==0) atmeans
marginsplot, recast(bar) title("") ytitle(Predicted Vote Share, margin(medium)) xtitle("") xlabel(0 "Nonworker" 1 "Worker") yscale(range(0(.2)1)) ylabel(0 0.2 0.4 0.6 0.8 1) xscale(range(-0.75(0.5)1.75)) graphregion(fcolor(white)) ciopts(col(gs4)) plot1opts(barw(0.3) color(gs8))

***********
*Figure 2
***********

*panel A
quietly melogit winwin i.worker_current_recode || raceid:, vce(cluster raceid)
margins i.worker_current_recode
marginsplot, recast(bar) title("") ytitle(Predicted Probability of Win, margin(medium)) xtitle("") xlabel(0 "Nonworker" 1 "Worker") yscale(range(0(.2)1)) ylabel(0 0.2 0.4 0.6 0.8 1) xscale(range(-0.75(0.5)1.75)) graphregion(fcolor(white)) ciopts(col(gs4)) plot1opts(barw(0.3) color(gs8))

*panel B
*note: Stata 15.1 could not calculate margins on multilevel logit model with controls. Stata 16.1 seems to handle it fine.
quietly melogit winwin i.worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
margins, at(worker_current_recode==(0 1) qualnumber==1 openseat==1 quality_cand==0 third==0) atmeans
marginsplot, recast(bar) title("") ytitle(Predicted Probability of Win, margin(medium)) xtitle("") xlabel(0 "Nonworker" 1 "Worker") yscale(range(0(.2)1)) ylabel(0 0.2 0.4 0.6 0.8 1) xscale(range(-0.75(0.5)1.75)) graphregion(fcolor(white)) ciopts(col(gs4)) plot1opts(barw(0.3) color(gs8))

***********
*Table 2
***********

ttest candpct if qualbin==0, by(worker_current_recode)
ttest candpct if qualbin==1, by(worker_current_recode)

***********
*Table A2
***********

mixed candpct worker_current_recode || raceid:, vce(cluster raceid)
estat ic
mixed candpct worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
*note: for the quality_cand variable, a value of 0 = nonquality nonincumbent, 1 = quality nonincumbent, 2 = incumbent

***********
*Table A3
***********

gen normvote = candvotes / (tvotes/candnumb)
regress normvote worker_current_recode i.year
regress normvote worker_current_recode qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote i.year

***********
*Table A4
***********

melogit winwin worker_current_recode || raceid:, vce(cluster raceid)
estat ic
melogit winwin worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

***********
*Table A5
***********

*install Stata package if you haven't already:
*ssc install cem

*separate "quality_cand" into unique variables for matching purposes
*generate indicator for quality non-incumbents
gen qni = quality_cand
recode qni (2=0)

*generate indicator for incumbents
gen incumbent = quality_cand
recode incumbent (1=0) (2=1)

*match on covariates in fully specified model
cem candnumb qualnumber openseat qni incumbent total_primary third dist_income college dem_vote, tr(worker_current_recode)

*regress on weighted data
reg candpct worker_current_recode [iweight=cem_weights]
estat ic
logit winwin worker_current_recode [iweight=cem_weights]
estat ic


***********
*Table A6
***********

gen currform = .
replace currform = 1 if worker_current_recode == 1
replace currform = 0 if worker_current_recode == 0 & worker == 1
ttest candpct, by(currform)

***********
*Table A7
***********

replace former_worker = 0 if worker_current_recode == 0 | worker_current_recode == 1
replace former_worker = 1 if worker_current_recode == 0 & worker == 1
label var former_worker "Former Worker"

*columns 1 and 2
mixed candpct i.worker_current_recode former_worker  || raceid:, vce(cluster raceid)
estat ic
mixed candpct worker_current_recode former_worker candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

*columns 3 and 4
melogit winwin worker_current_recode former_worker  || raceid:, vce(cluster raceid)
estat ic
melogit winwin worker_current_recode former_worker  candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

***********
*Table A8
***********

mixed candpct worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary gop third dist_income college dem_vote ideo_extreme || raceid:, vce(cluster raceid)
estat ic
melogit winwin worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary gop third dist_income college dem_vote ideo_extreme || raceid:, vce(cluster raceid)
estat ic

***********
*Table A9
***********

recode total_primary (0=1), gen(transform)
gen logfund = ln(transform)
label var logfund "Log Primary Funds"

recode total (.=.) (0=0) (*=1), gen(fundbin)
label var fundbin "Raised > $0"

tab fundbin worker_current_recode, col

mixed candpct i.worker_current_recode total_primary candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##c.total_primary candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode logfund candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##c.logfund candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode fundbin candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##i.fundbin candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

***********
*Table A10
***********

melogit winwin i.worker_current_recode total_primary candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##c.total_primary candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode logfund candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##c.logfund candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode fundbin candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.fundbin candnumb qualnumber openseat i.quality_cand  third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

***********
*Table A11
***********

gen partydonor = PartyDonors1
recode partydonor (.=0)
label var partydonor "Party Donors"
mixed candpct worker_current_recode partydonor candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##c.partydonor candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin worker_current_recode partydonor candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##c.partydonor candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

***********
*Table A12
***********
label var qualbin "Quality Candidate"
label var inc "Incumbent"

mixed candpct i.worker_current_recode##i.qualbin || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##i.qualbin candnumb qualnumber inc openseat total_primary third dist_income college dem_vote  || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.qualbin || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.qualbin candnumb qualnumber inc openseat total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic

***********
*Table B1
***********

tab cand_party worker_current_recode, col

***********
*Table B2
***********

mixed candpct i.worker_current_recode##i.gop if third==0 || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##i.gop candnumb qualnumber openseat i.quality_cand total_primary dist_income college dem_vote if third==0 || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.gop if third==0 || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.gop candnumb qualnumber openseat i.quality_cand total_primary dist_income college dem_vote if third==0 || raceid:, vce(cluster raceid)
estat ic

***********
*Table B3
***********

mixed candpct i.worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary dist_income college dem_vote if cand_party=="Republican" || raceid:, vce(cluster raceid) 
estat ic
mixed candpct i.worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary dist_income college dem_vote if cand_party=="Democrat" || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary dist_income college dem_vote if cand_party=="Republican" || raceid:, vce(cluster raceid)
estat ic
*note: Stata 15.1 had trouble estimating model below. Use 16.1 or later.
melogit winwin i.worker_current_recode candnumb qualnumber openseat i.quality_cand total_primary dist_income college dem_vote if cand_party=="Democrat" || raceid:, vce(cluster raceid)
estat ic

***********
*Table C1
***********

gen toptwo = 0
replace toptwo = 1 if state_abbrev == "WA"
replace toptwo = 1 if state_abbrev == "CA" & year > 2011

mixed candpct i.worker_current_recode##i.toptwo || raceid:, vce(cluster raceid)
estat ic
mixed candpct i.worker_current_recode##i.toptwo candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.toptwo || raceid:, vce(cluster raceid)
estat ic
melogit winwin i.worker_current_recode##i.toptwo candnumb qualnumber openseat i.quality_cand total_primary third dist_income college dem_vote || raceid:, vce(cluster raceid)
estat ic





